package cn.edu.njnu.opengmpback.user.service;

import cn.edu.njnu.opengmpback.Modeling.dao.ProjectDao;
import cn.edu.njnu.opengmpback.Modeling.entity.Project;
import cn.edu.njnu.opengmpback.Modeling.service.ProjectService;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.user.dao.HistoryDao;
import cn.edu.njnu.opengmpback.user.dao.UserDao;
import cn.edu.njnu.opengmpback.user.entity.History;
import cn.edu.njnu.opengmpback.user.entity.UserEntity;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * @Description
 * @Author wyjq
 * @Date 2022/3/17
 */

@Slf4j
@Service
public class HistoryService {

    @Autowired
    HistoryDao historyDao;

    @Autowired
    ProjectDao projectDao;

    @Autowired
    UserDao userDao;

    public History createHistory(String type, String content, String userId, String projectId,String state){
        History history = new History();
        history.setUserId(userId);
        history.setType(type);
        history.setContent(content);
        history.setProjectId(projectId);
        history.setState(state);
        return historyDao.save(history);
    }

    public JsonResult UserJoinProject(String hid, String state) {
        try {
            Optional<History> optionalHistory = historyDao.findById(hid);
            if(!optionalHistory.isPresent()){
                return ResultUtils.error(-1, "邀请不存在");
            }
            History history = optionalHistory.get();
            history.setState(state);
            historyDao.save(history);

            UserEntity user = userDao.findByUserId(history.getUserId());

            Optional<Project> optionalProject = projectDao.findById(history.getProjectId());
            if(!optionalProject.isPresent()){
                return ResultUtils.error(-1, "项目不存在");
            }
            Project project = optionalProject.get();

            if(state.equals("同意")){
                if (!user.getJoinedProjects().contains(history.getProjectId())) {
                    user.getJoinedProjects().add(history.getProjectId());
                }
                userDao.save(user);

                project.getMemberList().add(user.getUserId());
                projectDao.save(project);
                createHistory("邀请反馈", user.getName() + " 接受邀请, 加入了项目 " + project.getName(), project.getCreatorId(), history.getProjectId(), "");
            }else if(!state.equals("未读")){
                createHistory("邀请反馈", user.getName() + " 拒绝了加入项目 " + project.getName() + " 的邀请", project.getCreatorId(), history.getProjectId(), "");
            }

            return ResultUtils.success("操作成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtils.error(-1, "操作失败");
        }

    }
}
